//index.js
import productService from '../../service/product/index'

Page({
  data: {
    swiperItems: [],
    indexCate: [],
    youlikes: [],
    pageNo:1,
    pageSize:10,
    indexCateAndProducts: [],
    top: 0
  },
  onLoad: function () {
    // 加载轮播
    wx.request({
      url : 'http://grace.hcoder.net/api/swipers',
      success: (res) => { this.setData({ swiperItems: res.data.data });}
    });
    // 加载首页推荐图标
    wx.request({
      url : 'http://grace.hcoder.net/api/cate/indexCate',
      success : (res) => { this.setData({ indexCate: res.data.data });}
    });
    // 猜你喜欢
    this.youlike();
    // 加载首页推荐分类及产品
    wx.request({
      url : 'http://grace.hcoder.net/api/products/indexTj',
      success: (res) => {this.setData({ indexCateAndProducts: res.data.data});}
    });
  },
  // 搜索相关函数
  inputting: function (e) { console.log(e); },
  confirm: function (e) { console.log(e); },
  // 猜你喜欢
  youlike: function () {
    var {pageNo,pageSize,youlikes} = this.data;
    productService.page({pageNo,pageSize}).then(res=>{
      var results = res.data.records;
      var newdatas  = youlikes.concat(results)
      console.log("----------------->results",results)
      this.setData({youlikes:newdatas,pageNo:++pageNo});
    });
  },
  // 打开商品详情
  openProductInfo: function (e) {
    console.log("e=------------------>",e)
    var  {opid} = e.currentTarget.dataset;
    wx.navigateTo({ url: '../productInfo/productInfo?opid='+opid });
  },
  onReachBottom(){
    this.youlike();
  }
})
